<?php  

	namespace Home\Controller;
	use Think\Controller;
	//信息回收管理
	//@author  程传民
	class RecoveryController extends AllowController{

		public function index(){
			$users = D("users");

			//判断是否使用了按姓名查找
			// $arr1['role_id'] = array("GT",7);		//角色id值要大于7的才是学员(班长，组长，学员)
			$arr1 = array('can_login'=>0);			//新用户

			if(!empty($_GET['name'])){
				if(!empty($_GET['class'])){
					$arr2 = array("class"=>$_GET['class']);
					$tot = $users->where($arr1)->where($arr2)->where("name like '%".$_GET['name']."%'")->count();
				}else{
					$tot = $users->where($arr1)->where("name like '%".$_GET['name']."%'")->count();
				}
			}else{
				if(!empty($_GET['class'])){
					$arr2 = array("class"=>$_GET['class']);
					$tot = $users->where($arr1)->where($arr2)->count();
				}else{
					$tot = $users->where($arr1)->count();
				}
			}

	    	//分页管理
	    	$nums = 5;
	    	$page = new \Think\Page($tot,$nums);
	    	//分页管理中判断是否使用了按姓名查找   班级查找
	    	$search="";
	 		if(!empty($_GET['name'])){

	 			if(!empty($_GET['class'])){

	 				$arr2 = array("class"=>$_GET['class']);
	 				$list = $users->where($arr1)->where($arr2)->where("name like '%".$_GET['name']."%'")->limit($page->firstRow,$page->listRows)->select();
	 				$search="/name/{$_GET['name']}/class/{$_GET['class']}";
	 			}else{
	    			$list = $users->where($arr1)->where("name like '%".$_GET['name']."%'")->limit($page->firstRow,$page->listRows)->select();
	    			$search="/name/{$_GET['name']}";
	 			}
	 		}else{
	 			if(!empty($_GET['class'])){
	 				$arr2 = array("class"=>$_GET['class']);
	 				$list = $users->where($arr1)->where($arr2)->limit($page->firstRow,$page->listRows)->select();
	 				$search="/class/{$_GET['class']}";
		 		}else{
		 			$list = $users->where($arr1)->limit($page->firstRow,$page->listRows)->select();
		 		}
	 		}


			$class = D("class");
			$classs = $class->select();
			$role = D("role");
			$roles = $role->select();
			$team = D("team");
			$teams = $team->select();
			foreach($list as $key=>$value){
				//角色
				foreach($roles as $k=>$v){
					if($list[$key]['role_id']==$v['id']){
						$list[$key]['role_id']=$v['name'];
					}
				}
				//班级
				if($list[$key]['class']!=0){
					foreach($classs as $k=>$v){
						if($list[$key]['class']==$v['id']){
							$list[$key]['class'] = $v['name'];
						}
					}
				}else{
					$list[$key]['class'] = "暂未编入班级";
				}
				//小组
				if($list[$key]['team']!=0){
					foreach($teams as $k=>$v){
						if($list[$key]['team']==$v['id']){
							$list[$key]['team'] = $v['name'];
						}
					}
				}else{
					$list[$key]['team'] = "暂未编入小组";
				}
				$list[$key]['can_login'] = "禁用";
			}

			// 接受当前页,默认打开为1
	    	if (empty($_GET['p'])) {
	    		$p = "1";
	    	}else{
	    		$p = $_GET['p'];
	    	}
	    	// 分配总条数
			$this->assign("total",$tot);
			// 分配进一取整后的总页数
			$this->assign("page",ceil($tot/$nums));
			// 分配当前页的页数
			$this->assign("curr",$p);
			$this->assign("list",$list);
			$this->assign("search",$search);
			$this->assign("classs",$classs);
			$this->display("Recovery/recovery");
		}

		//用户启用（单个启用）
		public function start(){
			$arr = array("id"=>$_POST["id"]);
			// $arr = array("id"=>5);
			$a = array("can_login"=>2);
			$mod = D("users");
			$info = $mod->where($arr)->save($a);
			if($info){
				echo "启用成功";
			}else{
				echo "失败";
			}
		}

		//批量启用
		public function action(){
			$users = D("users");
			$arr = $_GET['ids'];

			$login["can_login"]=2;
			// $as = "";
			foreach($arr as $key=>$value){
				$arrs = array("id"=>$value);
				$info = $users->where($arrs)->save($login);
				// $as.=$value;
			}
			// echo $as;
			echo "成功";
		}

		//假条管理

		//已同意请假的假条
		public function notepass(){
			$leave = D("leave");
			$users = D("users");

			$tot = $leave->where("status>=4")->count();

			// 分页管理
			$nums = 5;
	    	$page = new \Think\Page($tot,5);
	    	
			$list = $leave->where("status>=4")->limit($page->firstRow,$page->listRows)->select();
			foreach($list as $key => $value){
				$arrs = array("id" => $value['pid']);
				$userinfo = $users->where($arrs)->find();
				$list[$key]['pid'] = $userinfo['name'];
				$list[$key]['status'] = "已生效";
				$list[$key]['color'] = "#01AAED";

				// 请假天数
				$list[$key]['day'] = ceil(($list[$key]['end_time']-$list[$key]['start_time'])/(24*60*60));
				//开始时间
				$list[$key]['start_time'] = date("Y-m-d",$list[$key]['start_time']);
				//结束时间
				$list[$key]['end_time'] = date("Y-m-d",$list[$key]['end_time']);

			}

			// 接受当前页,默认打开为1
	    	if (empty($_GET['p'])) {
	    		$p = "1";
	    	}else{
	    		$p = $_GET['p'];
	    	}
	    	// 分配总条数
			$this->assign("total",$tot);
			// 分配进一取整后的总页数
			$this->assign("page",ceil($tot/$nums));
			// 分配当前页的页数
			$this->assign("curr",$p);
			$this->assign("list",$list);
			$this->display("Recovery/notepass");
		}

		//已拒绝请假的假条
		public function notenotpass(){
			$leave = D("leave");
			$users = D("users");
			$arr = array("status"=>0);

			$tot = $leave->where($arr)->count();

			// 分页管理
			$nums = 5;
	    	$page = new \Think\Page($tot,5);
	    	
			$list = $leave->where($arr)->limit($page->firstRow,$page->listRows)->select();
			foreach($list as $key => $value){
				$arrs = array("id" => $value['pid']);
				$userinfo = $users->where($arrs)->find();
				$list[$key]['pid'] = $userinfo['name'];
				$list[$key]['status'] = "未生效";
				$list[$key]['color'] = "#01AAED";

				// 请假天数
				$list[$key]['day'] = ceil(($list[$key]['end_time']-$list[$key]['start_time'])/(24*60*60));
				//开始时间
				$list[$key]['start_time'] = date("Y-m-d",$list[$key]['start_time']);
				//结束时间
				$list[$key]['end_time'] = date("Y-m-d",$list[$key]['end_time']);

			}

			// 接受当前页,默认打开为1
	    	if (empty($_GET['p'])) {
	    		$p = "1";
	    	}else{
	    		$p = $_GET['p'];
	    	}
	    	// 分配总条数
			$this->assign("total",$tot);
			// 分配进一取整后的总页数
			$this->assign("page",ceil($tot/$nums));
			// 分配当前页的页数
			$this->assign("curr",$p);
			$this->assign("list",$list);
			$this->display("Recovery/notenotpass");
		}


		//绩效分低于零
		public function fail(){
			//绩效分小于0
			$arr1['performance'] = array("ELT",0);
			//登录状态不为0
			$arr2['can_login'] = array("NEQ",0);

			$users = D("users");
			$class = D("class");

			
			//姓名查找
			if(!empty($_GET['name'])){
				//班级查找
				if(!empty($_GET['class'])){
					$arrs = array("class"=>$_GET['class']);
					$tot = $users->where($arr1)->where($arr2)->where($arrs)->where("name like '%".$_GET['name']."%'")->count();
					
				}else{
					$tot = $users->where($arr1)->where($arr2)->where("name like '%".$_GET['name']."%'")->count();
					
				}
			}else{
				//班级查找
				if(!empty($_GET['class'])){
					$arrs = array("class"=>$_GET['class']);
					$tot = $users->where($arr1)->where($arr2)->where($arrs)->count();
					
				}else{
					$tot = $users->where($arr1)->where($arr2)->count();
				}
			}

			// $tot = $users->where($arr1)->where($arr2)->count();
			
			$nums = 5;
	    	$page = new \Think\Page($tot,$nums);
	    	$search = "";
	    	//姓名查找
			if(!empty($_GET['name'])){
				//班级查找
				if(!empty($_GET['class'])){
					$arrs = array("class"=>$_GET['class']);
					$list = $users->where($arr1)->where($arr2)->where($arrs)->where("name like '%".$_GET['name']."%'")->select();
					$search="/name/{$_GET['name']}/class/{$_GET['class']}";
				}else{
					$list = $users->where($arr1)->where($arr2)->where("name like '%".$_GET['name']."%'")->select();
					$search="/name/{$_GET['name']}";
				}
			}else{
				//班级查找
				if(!empty($_GET['class'])){
					$arrs = array("class"=>$_GET['class']);
					$list = $users->where($arr1)->where($arr2)->where($arrs)->select();
					$search="/class/{$_GET['class']}";
				}else{
					$list = $users->where($arr1)->where($arr2)->select();
				}
			}
			
			$classs = $class->select();

			foreach($list as $key=>$value){
				$arr4 = array("id"=>$value['class']);
				$info1 = $class->where($arr4)->find();
				$list[$key]['class'] = $info1['name'];

				if($value['team']==0){
					$list[$key]['team'] = "暂未分组";
				}else{
					$team = D("team");
					$arr5 = array("id"=>$value['team']);
					$info2 = $team->where($arr5)->find();
					$list[$key]['team'] = $info2['name'];
				}

				//角色
				$role = D("role");
				$arr5 = array("id"=>$value['role_id']);
				$info3 = $role->where($arr5)->find();

				$list[$key]['role_id'] = $info3['name'];

			}

			// var_dump($list);
			// exit;
			// 接受当前页,默认打开为1
	    	if (empty($_GET['p'])) {
	    		$p = "1";
	    	}else{
	    		$p = $_GET['p'];
	    	}
	    	// 分配总条数
			$this->assign("total",$tot);
			// 分配进一取整后的总页数
			$this->assign("page",ceil($tot/$nums));
			// 分配当前页的页数
			$this->assign("curr",$p);

			$this->assign("list",$list);
			$this->assign("search",$search);
			$this->assign("classs",$classs);
			$this->display("Recovery/fail");
		}

	}

?>